﻿using System;
using System.Diagnostics.Contracts;

namespace AnthStat.Infrastructure.Statistics
{
    public static class WHO2007
    {
        #region WHO 2007 Reference Data
        private static readonly double[,] WHO2007BMI = {

{ 1,0,-0.3053,13.4069,0.0956 },
{ 1,1,0.2708,14.9441,0.09027 },
{ 1,2,0.1118,16.3195,0.08677 },
{ 1,3,0.0068,16.8987,0.08495 },
{ 1,4,-0.0727,17.1579,0.08378 },
{ 1,5,-0.137,17.2919,0.08296 },
{ 1,6,-0.1913,17.3422,0.08234 },
{ 1,7,-0.2385,17.3288,0.08183 },
{ 1,8,-0.2802,17.2647,0.0814 },
{ 1,9,-0.3176,17.1662,0.08102 },
{ 1,10,-0.3516,17.0488,0.08068 },
{ 1,11,-0.3828,16.9239,0.08037 },
{ 1,12,-0.4115,16.7981,0.08009 },
{ 1,13,-0.4382,16.6743,0.07982 },
{ 1,14,-0.463,16.5548,0.07958 },
{ 1,15,-0.4863,16.4409,0.07935 },
{ 1,16,-0.5082,16.3335,0.07913 },
{ 1,17,-0.5289,16.2329,0.07892 },
{ 1,18,-0.5484,16.1392,0.07873 },
{ 1,19,-0.5669,16.0528,0.07854 },
{ 1,20,-0.5846,15.9743,0.07836 },
{ 1,21,-0.6014,15.9039,0.07818 },
{ 1,22,-0.6174,15.8412,0.07802 },
{ 1,23,-0.6328,15.7852,0.07786 },
{ 1,24,-0.6473,15.7356,0.07771 },
{ 1,24,-0.6187,16.0189,0.07785 },
{ 1,25,-0.584,15.98,0.07792 },
{ 1,26,-0.5497,15.9414,0.078 },
{ 1,27,-0.5166,15.9036,0.07808 },
{ 1,28,-0.485,15.8667,0.07818 },
{ 1,29,-0.4552,15.8306,0.07829 },
{ 1,30,-0.4274,15.7953,0.07841 },
{ 1,31,-0.4016,15.7606,0.07854 },
{ 1,32,-0.3782,15.7267,0.07867 },
{ 1,33,-0.3572,15.6934,0.07882 },
{ 1,34,-0.3388,15.661,0.07897 },
{ 1,35,-0.3231,15.6294,0.07914 },
{ 1,36,-0.3101,15.5988,0.07931 },
{ 1,37,-0.3,15.5693,0.0795 },
{ 1,38,-0.2927,15.541,0.07969 },
{ 1,39,-0.2884,15.514,0.0799 },
{ 1,40,-0.2869,15.4885,0.08012 },
{ 1,41,-0.2881,15.4645,0.08036 },
{ 1,42,-0.2919,15.442,0.08061 },
{ 1,43,-0.2981,15.421,0.08087 },
{ 1,44,-0.3067,15.4013,0.08115 },
{ 1,45,-0.3174,15.3827,0.08144 },
{ 1,46,-0.3303,15.3652,0.08174 },
{ 1,47,-0.3452,15.3485,0.08205 },
{ 1,48,-0.3622,15.3326,0.08238 },
{ 1,49,-0.3811,15.3174,0.08272 },
{ 1,50,-0.4019,15.3029,0.08307 },
{ 1,51,-0.4245,15.2891,0.08343 },
{ 1,52,-0.4488,15.2759,0.0838 },
{ 1,53,-0.4747,15.2633,0.08418 },
{ 1,54,-0.5019,15.2514,0.08457 },
{ 1,55,-0.5303,15.24,0.08496 },
{ 1,56,-0.5599,15.2291,0.08536 },
{ 1,57,-0.5905,15.2188,0.08577 },
{ 1,58,-0.6223,15.2091,0.08617 },
{ 1,59,-0.6552,15.2,0.08659 },
{ 1,60,-0.6892,15.1916,0.087 },                                              
{ 1,61,-0.7387,15.2641,0.0839 },
{ 1,62,-0.7621,15.2616,0.08414 },
{ 1,63,-0.7856,15.2604,0.08439 },
{ 1,64,-0.8089,15.2605,0.08464 },
{ 1,65,-0.8322,15.2619,0.0849 },
{ 1,66,-0.8554,15.2645,0.08516 },
{ 1,67,-0.8785,15.2684,0.08543 },
{ 1,68,-0.9015,15.2737,0.0857 },
{ 1,69,-0.9243,15.2801,0.08597 },
{ 1,70,-0.9471,15.2877,0.08625 },
{ 1,71,-0.9697,15.2965,0.08653 },
{ 1,72,-0.9921,15.3062,0.08682 },
{ 1,73,-1.0144,15.3169,0.08711 },
{ 1,74,-1.0365,15.3285,0.08741 },
{ 1,75,-1.0584,15.3408,0.08771 },
{ 1,76,-1.0801,15.354,0.08802 },
{ 1,77,-1.1017,15.3679,0.08833 },
{ 1,78,-1.123,15.3825,0.08865 },
{ 1,79,-1.1441,15.3978,0.08898 },
{ 1,80,-1.1649,15.4137,0.08931 },
{ 1,81,-1.1856,15.4302,0.08964 },
{ 1,82,-1.206,15.4473,0.08998 },
{ 1,83,-1.2261,15.465,0.09033 },
{ 1,84,-1.246,15.4832,0.09068 },
{ 1,85,-1.2656,15.5019,0.09103 },
{ 1,86,-1.2849,15.521,0.09139 },
{ 1,87,-1.304,15.5407,0.09176 },
{ 1,88,-1.3228,15.5608,0.09213 },
{ 1,89,-1.3414,15.5814,0.09251 },
{ 1,90,-1.3596,15.6023,0.09289 },
{ 1,91,-1.3776,15.6237,0.09327 },
{ 1,92,-1.3953,15.6455,0.09366 },
{ 1,93,-1.4126,15.6677,0.09406 },
{ 1,94,-1.4297,15.6903,0.09445 },
{ 1,95,-1.4464,15.7133,0.09486 },
{ 1,96,-1.4629,15.7368,0.09526 },
{ 1,97,-1.479,15.7606,0.09567 },
{ 1,98,-1.4947,15.7848,0.09609 },
{ 1,99,-1.5101,15.8094,0.09651 },
{ 1,100,-1.5252,15.8344,0.09693 },
{ 1,101,-1.5399,15.8597,0.09735 },
{ 1,102,-1.5542,15.8855,0.09778 },
{ 1,103,-1.5681,15.9116,0.09821 },
{ 1,104,-1.5817,15.9381,0.09864 },
{ 1,105,-1.5948,15.9651,0.09907 },
{ 1,106,-1.6076,15.9925,0.09951 },
{ 1,107,-1.6199,16.0205,0.09994 },
{ 1,108,-1.6318,16.049,0.10038 },
{ 1,109,-1.6433,16.0781,0.10082 },
{ 1,110,-1.6544,16.1078,0.10126 },
{ 1,111,-1.6651,16.1381,0.1017 },
{ 1,112,-1.6753,16.1692,0.10214 },
{ 1,113,-1.6851,16.2009,0.10259 },
{ 1,114,-1.6944,16.2333,0.10303 },
{ 1,115,-1.7032,16.2665,0.10347 },
{ 1,116,-1.7116,16.3004,0.10391 },
{ 1,117,-1.7196,16.3351,0.10435 },
{ 1,118,-1.7271,16.3704,0.10478 },
{ 1,119,-1.7341,16.4065,0.10522 },
{ 1,120,-1.7407,16.4433,0.10566 },
{ 1,121,-1.7468,16.4807,0.10609 },
{ 1,122,-1.7525,16.5189,0.10652 },
{ 1,123,-1.7578,16.5578,0.10695 },
{ 1,124,-1.7626,16.5974,0.10738 },
{ 1,125,-1.767,16.6376,0.1078 },
{ 1,126,-1.771,16.6786,0.10823 },
{ 1,127,-1.7745,16.7203,0.10865 },
{ 1,128,-1.7777,16.7628,0.10906 },
{ 1,129,-1.7804,16.8059,0.10948 },
{ 1,130,-1.7828,16.8497,0.10989 },
{ 1,131,-1.7847,16.8941,0.1103 },
{ 1,132,-1.7862,16.9392,0.1107 },
{ 1,133,-1.7873,16.985,0.1111 },
{ 1,134,-1.7881,17.0314,0.1115 },
{ 1,135,-1.7884,17.0784,0.11189 },
{ 1,136,-1.7884,17.1262,0.11228 },
{ 1,137,-1.788,17.1746,0.11266 },
{ 1,138,-1.7873,17.2236,0.11304 },
{ 1,139,-1.7861,17.2734,0.11342 },
{ 1,140,-1.7846,17.324,0.11379 },
{ 1,141,-1.7828,17.3752,0.11415 },
{ 1,142,-1.7806,17.4272,0.11451 },
{ 1,143,-1.778,17.4799,0.11487 },
{ 1,144,-1.7751,17.5334,0.11522 },
{ 1,145,-1.7719,17.5877,0.11556 },
{ 1,146,-1.7684,17.6427,0.1159 },
{ 1,147,-1.7645,17.6985,0.11623 },
{ 1,148,-1.7604,17.7551,0.11656 },
{ 1,149,-1.7559,17.8124,0.11688 },
{ 1,150,-1.7511,17.8704,0.1172 },
{ 1,151,-1.7461,17.9292,0.11751 },
{ 1,152,-1.7408,17.9887,0.11781 },
{ 1,153,-1.7352,18.0488,0.11811 },
{ 1,154,-1.7293,18.1096,0.11841 },
{ 1,155,-1.7232,18.171,0.11869 },
{ 1,156,-1.7168,18.233,0.11898 },
{ 1,157,-1.7102,18.2955,0.11925 },
{ 1,158,-1.7033,18.3586,0.11952 },
{ 1,159,-1.6962,18.4221,0.11979 },
{ 1,160,-1.6888,18.486,0.12005 },
{ 1,161,-1.6811,18.5502,0.1203 },
{ 1,162,-1.6732,18.6148,0.12055 },
{ 1,163,-1.6651,18.6795,0.12079 },
{ 1,164,-1.6568,18.7445,0.12102 },
{ 1,165,-1.6482,18.8095,0.12125 },
{ 1,166,-1.6394,18.8746,0.12148 },
{ 1,167,-1.6304,18.9398,0.1217 },
{ 1,168,-1.6211,19.005,0.12191 },
{ 1,169,-1.6116,19.0701,0.12212 },
{ 1,170,-1.602,19.1351,0.12233 },
{ 1,171,-1.5921,19.2,0.12253 },
{ 1,172,-1.5821,19.2648,0.12272 },
{ 1,173,-1.5719,19.3294,0.12291 },
{ 1,174,-1.5615,19.3937,0.1231 },
{ 1,175,-1.551,19.4578,0.12328 },
{ 1,176,-1.5403,19.5217,0.12346 },
{ 1,177,-1.5294,19.5853,0.12363 },
{ 1,178,-1.5185,19.6486,0.1238 },
{ 1,179,-1.5074,19.7117,0.12396 },
{ 1,180,-1.4961,19.7744,0.12412 },
{ 1,181,-1.4848,19.8367,0.12428 },
{ 1,182,-1.4733,19.8987,0.12443 },
{ 1,183,-1.4617,19.9603,0.12458 },
{ 1,184,-1.45,20.0215,0.12473 },
{ 1,185,-1.4382,20.0823,0.12487 },
{ 1,186,-1.4263,20.1427,0.12501 },
{ 1,187,-1.4143,20.2026,0.12514 },
{ 1,188,-1.4022,20.2621,0.12528 },
{ 1,189,-1.39,20.3211,0.12541 },
{ 1,190,-1.3777,20.3796,0.12554 },
{ 1,191,-1.3653,20.4376,0.12567 },
{ 1,192,-1.3529,20.4951,0.12579 },
{ 1,193,-1.3403,20.5521,0.12591 },
{ 1,194,-1.3277,20.6085,0.12603 },
{ 1,195,-1.3149,20.6644,0.12615 },
{ 1,196,-1.3021,20.7197,0.12627 },
{ 1,197,-1.2892,20.7745,0.12638 },
{ 1,198,-1.2762,20.8287,0.1265 },
{ 1,199,-1.2631,20.8824,0.12661 },
{ 1,200,-1.2499,20.9355,0.12672 },
{ 1,201,-1.2366,20.9881,0.12683 },
{ 1,202,-1.2233,21.04,0.12694 },
{ 1,203,-1.2098,21.0914,0.12704 },
{ 1,204,-1.1962,21.1423,0.12715 },
{ 1,205,-1.1826,21.1925,0.12726 },
{ 1,206,-1.1688,21.2423,0.12736 },
{ 1,207,-1.155,21.2914,0.12746 },
{ 1,208,-1.141,21.34,0.12756 },
{ 1,209,-1.127,21.388,0.12767 },
{ 1,210,-1.1129,21.4354,0.12777 },
{ 1,211,-1.0986,21.4822,0.12787 },
{ 1,212,-1.0843,21.5285,0.12797 },
{ 1,213,-1.0699,21.5742,0.12807 },
{ 1,214,-1.0553,21.6193,0.12816 },
{ 1,215,-1.0407,21.6638,0.12826 },
{ 1,216,-1.026,21.7077,0.12836 },
{ 1,217,-1.0112,21.751,0.12845 },
{ 1,218,-0.9962,21.7937,0.12855 },
{ 1,219,-0.9812,21.8358,0.12864 },
{ 1,220,-0.9661,21.8773,0.12874 },
{ 1,221,-0.9509,21.9182,0.12883 },
{ 1,222,-0.9356,21.9585,0.12893 },
{ 1,223,-0.9202,21.9982,0.12902 },
{ 1,224,-0.9048,22.0374,0.12911 },
{ 1,225,-0.8892,22.076,0.1292 },
{ 1,226,-0.8735,22.114,0.1293 },
{ 1,227,-0.8578,22.1514,0.12939 },
{ 1,228,-0.8419,22.1883,0.12948 },
{ 2,0,-0.0631,13.3363,0.09272 },
{ 2,1,0.3448,14.5679,0.09556 },
{ 2,2,0.1749,15.7679,0.09371 },
{ 2,3,0.0643,16.3574,0.09254 },
{ 2,4,-0.0191,16.6703,0.09166 },
{ 2,5,-0.0864,16.8386,0.09096 },
{ 2,6,-0.1429,16.9083,0.09036 },
{ 2,7,-0.1916,16.902,0.08984 },
{ 2,8,-0.2344,16.8404,0.08939 },
{ 2,9,-0.2725,16.7406,0.08898 },
{ 2,10,-0.3068,16.6184,0.08861 },
{ 2,11,-0.3381,16.4875,0.08828 },
{ 2,12,-0.3667,16.3568,0.08797 },
{ 2,13,-0.3932,16.2311,0.08768 },
{ 2,14,-0.4177,16.1128,0.08741 },
{ 2,15,-0.4407,16.0028,0.08716 },
{ 2,16,-0.4623,15.9017,0.08693 },
{ 2,17,-0.4825,15.8096,0.08671 },
{ 2,18,-0.5017,15.7263,0.0865 },
{ 2,19,-0.5199,15.6517,0.0863 },
{ 2,20,-0.5372,15.5855,0.08612 },
{ 2,21,-0.5537,15.5278,0.08594 },
{ 2,22,-0.5695,15.4787,0.08577 },
{ 2,23,-0.5846,15.438,0.0856 },
{ 2,24,-0.5989,15.4052,0.08545 },
{ 2,24,-0.5684,15.6881,0.08454 },
{ 2,25,-0.5684,15.659,0.08452 },
{ 2,26,-0.5684,15.6308,0.08449 },
{ 2,27,-0.5684,15.6037,0.08446 },
{ 2,28,-0.5684,15.5777,0.08444 },
{ 2,29,-0.5684,15.5523,0.08443 },
{ 2,30,-0.5684,15.5276,0.08444 },
{ 2,31,-0.5684,15.5034,0.08448 },
{ 2,32,-0.5684,15.4798,0.08455 },
{ 2,33,-0.5684,15.4572,0.08467 },
{ 2,34,-0.5684,15.4356,0.08484 },
{ 2,35,-0.5684,15.4155,0.08506 },
{ 2,36,-0.5684,15.3968,0.08535 },
{ 2,37,-0.5684,15.3796,0.08569 },
{ 2,38,-0.5684,15.3638,0.08609 },
{ 2,39,-0.5684,15.3493,0.08654 },
{ 2,40,-0.5684,15.3358,0.08704 },
{ 2,41,-0.5684,15.3233,0.08757 },
{ 2,42,-0.5684,15.3116,0.08813 },
{ 2,43,-0.5684,15.3007,0.08872 },
{ 2,44,-0.5684,15.2905,0.08931 },
{ 2,45,-0.5684,15.2814,0.08991 },
{ 2,46,-0.5684,15.2732,0.09051 },
{ 2,47,-0.5684,15.2661,0.0911 },
{ 2,48,-0.5684,15.2602,0.09168 },
{ 2,49,-0.5684,15.2556,0.09227 },
{ 2,50,-0.5684,15.2523,0.09286 },
{ 2,51,-0.5684,15.2503,0.09345 },
{ 2,52,-0.5684,15.2496,0.09403 },
{ 2,53,-0.5684,15.2502,0.0946 },
{ 2,54,-0.5684,15.2519,0.09515 },
{ 2,55,-0.5684,15.2544,0.09568 },
{ 2,56,-0.5684,15.2575,0.09618 },
{ 2,57,-0.5684,15.2612,0.09665 },
{ 2,58,-0.5684,15.2653,0.09709 },
{ 2,59,-0.5684,15.2698,0.0975 },
{ 2,60,-0.5684,15.2747,0.09789 },
{ 2,61,-0.8886,15.2441,0.09692 },
{ 2,62,-0.9068,15.2434,0.09738 },
{ 2,63,-0.9248,15.2433,0.09783 },
{ 2,64,-0.9427,15.2438,0.09829 },
{ 2,65,-0.9605,15.2448,0.09875 },
{ 2,66,-0.978,15.2464,0.0992 },
{ 2,67,-0.9954,15.2487,0.09966 },
{ 2,68,-1.0126,15.2516,0.10012 },
{ 2,69,-1.0296,15.2551,0.10058 },
{ 2,70,-1.0464,15.2592,0.10104 },
{ 2,71,-1.063,15.2641,0.10149 },
{ 2,72,-1.0794,15.2697,0.10195 },
{ 2,73,-1.0956,15.276,0.10241 },
{ 2,74,-1.1115,15.2831,0.10287 },
{ 2,75,-1.1272,15.2911,0.10333 },
{ 2,76,-1.1427,15.2998,0.10379 },
{ 2,77,-1.1579,15.3095,0.10425 },
{ 2,78,-1.1728,15.32,0.10471 },
{ 2,79,-1.1875,15.3314,0.10517 },
{ 2,80,-1.2019,15.3439,0.10562 },
{ 2,81,-1.216,15.3572,0.10608 },
{ 2,82,-1.2298,15.3717,0.10654 },
{ 2,83,-1.2433,15.3871,0.107 },
{ 2,84,-1.2565,15.4036,0.10746 },
{ 2,85,-1.2693,15.4211,0.10792 },
{ 2,86,-1.2819,15.4397,0.10837 },
{ 2,87,-1.2941,15.4593,0.10883 },
{ 2,88,-1.306,15.4798,0.10929 },
{ 2,89,-1.3175,15.5014,0.10974 },
{ 2,90,-1.3287,15.524,0.1102 },
{ 2,91,-1.3395,15.5476,0.11065 },
{ 2,92,-1.3499,15.5723,0.1111 },
{ 2,93,-1.36,15.5979,0.11156 },
{ 2,94,-1.3697,15.6246,0.11201 },
{ 2,95,-1.379,15.6523,0.11246 },
{ 2,96,-1.388,15.681,0.11291 },
{ 2,97,-1.3966,15.7107,0.11335 },
{ 2,98,-1.4047,15.7415,0.1138 },
{ 2,99,-1.4125,15.7732,0.11424 },
{ 2,100,-1.4199,15.8058,0.11469 },
{ 2,101,-1.427,15.8394,0.11513 },
{ 2,102,-1.4336,15.8738,0.11557 },
{ 2,103,-1.4398,15.909,0.11601 },
{ 2,104,-1.4456,15.9451,0.11644 },
{ 2,105,-1.4511,15.9818,0.11688 },
{ 2,106,-1.4561,16.0194,0.11731 },
{ 2,107,-1.4607,16.0575,0.11774 },
{ 2,108,-1.465,16.0964,0.11816 },
{ 2,109,-1.4688,16.1358,0.11859 },
{ 2,110,-1.4723,16.1759,0.11901 },
{ 2,111,-1.4753,16.2166,0.11943 },
{ 2,112,-1.478,16.258,0.11985 },
{ 2,113,-1.4803,16.2999,0.12026 },
{ 2,114,-1.4823,16.3425,0.12067 },
{ 2,115,-1.4838,16.3858,0.12108 },
{ 2,116,-1.485,16.4298,0.12148 },
{ 2,117,-1.4859,16.4746,0.12188 },
{ 2,118,-1.4864,16.52,0.12228 },
{ 2,119,-1.4866,16.5663,0.12268 },
{ 2,120,-1.4864,16.6133,0.12307 },
{ 2,121,-1.4859,16.6612,0.12346 },
{ 2,122,-1.4851,16.71,0.12384 },
{ 2,123,-1.4839,16.7595,0.12422 },
{ 2,124,-1.4825,16.81,0.1246 },
{ 2,125,-1.4807,16.8614,0.12497 },
{ 2,126,-1.4787,16.9136,0.12534 },
{ 2,127,-1.4763,16.9667,0.12571 },
{ 2,128,-1.4737,17.0208,0.12607 },
{ 2,129,-1.4708,17.0757,0.12643 },
{ 2,130,-1.4677,17.1316,0.12678 },
{ 2,131,-1.4642,17.1883,0.12713 },
{ 2,132,-1.4606,17.2459,0.12748 },
{ 2,133,-1.4567,17.3044,0.12782 },
{ 2,134,-1.4526,17.3637,0.12816 },
{ 2,135,-1.4482,17.4238,0.12849 },
{ 2,136,-1.4436,17.4847,0.12882 },
{ 2,137,-1.4389,17.5464,0.12914 },
{ 2,138,-1.4339,17.6088,0.12946 },
{ 2,139,-1.4288,17.6719,0.12978 },
{ 2,140,-1.4235,17.7357,0.13009 },
{ 2,141,-1.418,17.8001,0.1304 },
{ 2,142,-1.4123,17.8651,0.1307 },
{ 2,143,-1.4065,17.9306,0.13099 },
{ 2,144,-1.4006,17.9966,0.13129 },
{ 2,145,-1.3945,18.063,0.13158 },
{ 2,146,-1.3883,18.1297,0.13186 },
{ 2,147,-1.3819,18.1967,0.13214 },
{ 2,148,-1.3755,18.2639,0.13241 },
{ 2,149,-1.3689,18.3312,0.13268 },
{ 2,150,-1.3621,18.3986,0.13295 },
{ 2,151,-1.3553,18.466,0.13321 },
{ 2,152,-1.3483,18.5333,0.13347 },
{ 2,153,-1.3413,18.6006,0.13372 },
{ 2,154,-1.3341,18.6677,0.13397 },
{ 2,155,-1.3269,18.7346,0.13421 },
{ 2,156,-1.3195,18.8012,0.13445 },
{ 2,157,-1.3121,18.8675,0.13469 },
{ 2,158,-1.3046,18.9335,0.13492 },
{ 2,159,-1.297,18.9991,0.13514 },
{ 2,160,-1.2894,19.0642,0.13537 },
{ 2,161,-1.2816,19.1289,0.13559 },
{ 2,162,-1.2739,19.1931,0.1358 },
{ 2,163,-1.2661,19.2567,0.13601 },
{ 2,164,-1.2583,19.3197,0.13622 },
{ 2,165,-1.2504,19.382,0.13642 },
{ 2,166,-1.2425,19.4437,0.13662 },
{ 2,167,-1.2345,19.5045,0.13681 },
{ 2,168,-1.2266,19.5647,0.137 },
{ 2,169,-1.2186,19.624,0.13719 },
{ 2,170,-1.2107,19.6824,0.13738 },
{ 2,171,-1.2027,19.74,0.13756 },
{ 2,172,-1.1947,19.7966,0.13774 },
{ 2,173,-1.1867,19.8523,0.13791 },
{ 2,174,-1.1788,19.907,0.13808 },
{ 2,175,-1.1708,19.9607,0.13825 },
{ 2,176,-1.1629,20.0133,0.13841 },
{ 2,177,-1.1549,20.0648,0.13858 },
{ 2,178,-1.147,20.1152,0.13873 },
{ 2,179,-1.139,20.1644,0.13889 },
{ 2,180,-1.1311,20.2125,0.13904 },
{ 2,181,-1.1232,20.2595,0.1392 },
{ 2,182,-1.1153,20.3053,0.13934 },
{ 2,183,-1.1074,20.3499,0.13949 },
{ 2,184,-1.0996,20.3934,0.13963 },
{ 2,185,-1.0917,20.4357,0.13977 },
{ 2,186,-1.0838,20.4769,0.13991 },
{ 2,187,-1.076,20.517,0.14005 },
{ 2,188,-1.0681,20.556,0.14018 },
{ 2,189,-1.0603,20.5938,0.14031 },
{ 2,190,-1.0525,20.6306,0.14044 },
{ 2,191,-1.0447,20.6663,0.14057 },
{ 2,192,-1.0368,20.7008,0.1407 },
{ 2,193,-1.029,20.7344,0.14082 },
{ 2,194,-1.0212,20.7668,0.14094 },
{ 2,195,-1.0134,20.7982,0.14106 },
{ 2,196,-1.0055,20.8286,0.14118 },
{ 2,197,-0.9977,20.858,0.1413 },
{ 2,198,-0.9898,20.8863,0.14142 },
{ 2,199,-0.9819,20.9137,0.14153 },
{ 2,200,-0.974,20.9401,0.14164 },
{ 2,201,-0.9661,20.9656,0.14176 },
{ 2,202,-0.9582,20.9901,0.14187 },
{ 2,203,-0.9503,21.0138,0.14198 },
{ 2,204,-0.9423,21.0367,0.14208 },
{ 2,205,-0.9344,21.0587,0.14219 },
{ 2,206,-0.9264,21.0801,0.1423 },
{ 2,207,-0.9184,21.1007,0.1424 },
{ 2,208,-0.9104,21.1206,0.1425 },
{ 2,209,-0.9024,21.1399,0.14261 },
{ 2,210,-0.8944,21.1586,0.14271 },
{ 2,211,-0.8863,21.1768,0.14281 },
{ 2,212,-0.8783,21.1944,0.14291 },
{ 2,213,-0.8703,21.2116,0.14301 },
{ 2,214,-0.8623,21.2282,0.14311 },
{ 2,215,-0.8542,21.2444,0.1432 },
{ 2,216,-0.8462,21.2603,0.1433 },
{ 2,217,-0.8382,21.2757,0.1434 },
{ 2,218,-0.8301,21.2908,0.14349 },
{ 2,219,-0.8221,21.3055,0.14359 },
{ 2,220,-0.814,21.32,0.14368 },
{ 2,221,-0.806,21.3341,0.14377 },
{ 2,222,-0.798,21.348,0.14386 },
{ 2,223,-0.7899,21.3617,0.14396 },
{ 2,224,-0.7819,21.3752,0.14405 },
{ 2,225,-0.7738,21.3884,0.14414 },
{ 2,226,-0.7658,21.4014,0.14423 },
{ 2,227,-0.7577,21.4143,0.14432 },
{ 2,228,-0.7496,21.4269,0.14441 }
                                              };
        private static readonly double[,] WHO2007HtAge = {
{ 1,0,1,49.8842,0.03795 },
{ 1,1,1,54.7244,0.03557 },
{ 1,2,1,58.4249,0.03424 },
{ 1,3,1,61.4292,0.03328 },
{ 1,4,1,63.886,0.03257 },
{ 1,5,1,65.9026,0.03204 },
{ 1,6,1,67.6236,0.03165 },
{ 1,7,1,69.1645,0.03139 },
{ 1,8,1,70.5994,0.03124 },
{ 1,9,1,71.9687,0.03117 },
{ 1,10,1,73.2812,0.03118 },
{ 1,11,1,74.5388,0.03125 },
{ 1,12,1,75.7488,0.03137 },
{ 1,13,1,76.9186,0.03154 },
{ 1,14,1,78.0497,0.03174 },
{ 1,15,1,79.1458,0.03197 },
{ 1,16,1,80.2113,0.03222 },
{ 1,17,1,81.2487,0.0325 },
{ 1,18,1,82.2587,0.03279 },
{ 1,19,1,83.2418,0.0331 },
{ 1,20,1,84.1996,0.03342 },
{ 1,21,1,85.1348,0.03376 },
{ 1,22,1,86.0477,0.0341 },
{ 1,23,1,86.941,0.03445 },
{ 1,24,1,87.8161,0.03479 },
{ 1,24,1,87.1161,0.03507 },
{ 1,25,1,87.972,0.03542 },
{ 1,26,1,88.8065,0.03576 },
{ 1,27,1,89.6197,0.0361 },
{ 1,28,1,90.412,0.03642 },
{ 1,29,1,91.1828,0.03674 },
{ 1,30,1,91.9327,0.03704 },
{ 1,31,1,92.6631,0.03733 },
{ 1,32,1,93.3753,0.03761 },
{ 1,33,1,94.0711,0.03787 },
{ 1,34,1,94.7532,0.03812 },
{ 1,35,1,95.4236,0.03836 },
{ 1,36,1,96.0835,0.03858 },
{ 1,37,1,96.7337,0.03879 },
{ 1,38,1,97.3749,0.039 },
{ 1,39,1,98.0073,0.03919 },
{ 1,40,1,98.631,0.03937 },
{ 1,41,1,99.2459,0.03954 },
{ 1,42,1,99.8515,0.03971 },
{ 1,43,1,100.4485,0.03986 },
{ 1,44,1,101.0374,0.04002 },
{ 1,45,1,101.6186,0.04016 },
{ 1,46,1,102.1933,0.04031 },
{ 1,47,1,102.7625,0.04045 },
{ 1,48,1,103.3273,0.04059 },
{ 1,49,1,103.8886,0.04073 },
{ 1,50,1,104.4473,0.04086 },
{ 1,51,1,105.0041,0.041 },
{ 1,52,1,105.5596,0.04113 },
{ 1,53,1,106.1138,0.04126 },
{ 1,54,1,106.6668,0.04139 },
{ 1,55,1,107.2188,0.04152 },
{ 1,56,1,107.7697,0.04165 },
{ 1,57,1,108.3198,0.04177 },
{ 1,58,1,108.8689,0.0419 },
{ 1,59,1,109.417,0.04202 },
{ 1,60,1,109.9638,0.04214 },
{ 1,61,1,110.2647,0.04164 },
{ 1,62,1,110.8006,0.04172 },
{ 1,63,1,111.3338,0.0418 },
{ 1,64,1,111.8636,0.04187 },
{ 1,65,1,112.3895,0.04195 },
{ 1,66,1,112.911,0.04203 },
{ 1,67,1,113.428,0.04211 },
{ 1,68,1,113.941,0.04218 },
{ 1,69,1,114.45,0.04226 },
{ 1,70,1,114.9547,0.04234 },
{ 1,71,1,115.4549,0.04241 },
{ 1,72,1,115.9509,0.04249 },
{ 1,73,1,116.4432,0.04257 },
{ 1,74,1,116.9325,0.04264 },
{ 1,75,1,117.4196,0.04272 },
{ 1,76,1,117.9046,0.0428 },
{ 1,77,1,118.388,0.04287 },
{ 1,78,1,118.87,0.04295 },
{ 1,79,1,119.3508,0.04303 },
{ 1,80,1,119.8303,0.04311 },
{ 1,81,1,120.3085,0.04318 },
{ 1,82,1,120.7853,0.04326 },
{ 1,83,1,121.2604,0.04334 },
{ 1,84,1,121.7338,0.04342 },
{ 1,85,1,122.2053,0.0435 },
{ 1,86,1,122.675,0.04358 },
{ 1,87,1,123.1429,0.04366 },
{ 1,88,1,123.6092,0.04374 },
{ 1,89,1,124.0736,0.04382 },
{ 1,90,1,124.5361,0.0439 },
{ 1,91,1,124.9964,0.04398 },
{ 1,92,1,125.4545,0.04406 },
{ 1,93,1,125.9104,0.04414 },
{ 1,94,1,126.364,0.04422 },
{ 1,95,1,126.8156,0.0443 },
{ 1,96,1,127.2651,0.04438 },
{ 1,97,1,127.7129,0.04446 },
{ 1,98,1,128.159,0.04454 },
{ 1,99,1,128.6034,0.04462 },
{ 1,100,1,129.0466,0.0447 },
{ 1,101,1,129.4887,0.04478 },
{ 1,102,1,129.93,0.04487 },
{ 1,103,1,130.3705,0.04495 },
{ 1,104,1,130.8103,0.04503 },
{ 1,105,1,131.2495,0.04511 },
{ 1,106,1,131.6884,0.04519 },
{ 1,107,1,132.1269,0.04527 },
{ 1,108,1,132.5652,0.04535 },
{ 1,109,1,133.0031,0.04543 },
{ 1,110,1,133.4404,0.04551 },
{ 1,111,1,133.877,0.04559 },
{ 1,112,1,134.313,0.04566 },
{ 1,113,1,134.7483,0.04574 },
{ 1,114,1,135.1829,0.04582 },
{ 1,115,1,135.6168,0.04589 },
{ 1,116,1,136.0501,0.04597 },
{ 1,117,1,136.4829,0.04604 },
{ 1,118,1,136.9153,0.04612 },
{ 1,119,1,137.3474,0.04619 },
{ 1,120,1,137.7795,0.04626 },
{ 1,121,1,138.2119,0.04633 },
{ 1,122,1,138.6452,0.0464 },
{ 1,123,1,139.0797,0.04647 },
{ 1,124,1,139.5158,0.04654 },
{ 1,125,1,139.954,0.04661 },
{ 1,126,1,140.3948,0.04667 },
{ 1,127,1,140.8387,0.04674 },
{ 1,128,1,141.2859,0.0468 },
{ 1,129,1,141.7368,0.04686 },
{ 1,130,1,142.1916,0.04692 },
{ 1,131,1,142.6501,0.04698 },
{ 1,132,1,143.1126,0.04703 },
{ 1,133,1,143.5795,0.04709 },
{ 1,134,1,144.0511,0.04714 },
{ 1,135,1,144.5276,0.04719 },
{ 1,136,1,145.0093,0.04723 },
{ 1,137,1,145.4964,0.04728 },
{ 1,138,1,145.9891,0.04732 },
{ 1,139,1,146.4878,0.04736 },
{ 1,140,1,146.9927,0.0474 },
{ 1,141,1,147.5041,0.04744 },
{ 1,142,1,148.0224,0.04747 },
{ 1,143,1,148.5478,0.0475 },
{ 1,144,1,149.0807,0.04753 },
{ 1,145,1,149.6212,0.04755 },
{ 1,146,1,150.1694,0.04758 },
{ 1,147,1,150.7256,0.04759 },
{ 1,148,1,151.2899,0.04761 },
{ 1,149,1,151.8623,0.04762 },
{ 1,150,1,152.4425,0.04763 },
{ 1,151,1,153.0298,0.04763 },
{ 1,152,1,153.6234,0.04764 },
{ 1,153,1,154.2223,0.04763 },
{ 1,154,1,154.8258,0.04763 },
{ 1,155,1,155.4329,0.04762 },
{ 1,156,1,156.0426,0.0476 },
{ 1,157,1,156.6539,0.04758 },
{ 1,158,1,157.266,0.04756 },
{ 1,159,1,157.8775,0.04754 },
{ 1,160,1,158.4871,0.04751 },
{ 1,161,1,159.0937,0.04747 },
{ 1,162,1,159.6962,0.04744 },
{ 1,163,1,160.2939,0.0474 },
{ 1,164,1,160.8861,0.04735 },
{ 1,165,1,161.472,0.0473 },
{ 1,166,1,162.0505,0.04725 },
{ 1,167,1,162.6207,0.0472 },
{ 1,168,1,163.1816,0.04714 },
{ 1,169,1,163.7321,0.04707 },
{ 1,170,1,164.2717,0.04701 },
{ 1,171,1,164.7994,0.04694 },
{ 1,172,1,165.3145,0.04687 },
{ 1,173,1,165.8165,0.04679 },
{ 1,174,1,166.305,0.04671 },
{ 1,175,1,166.7799,0.04663 },
{ 1,176,1,167.2415,0.04655 },
{ 1,177,1,167.6899,0.04646 },
{ 1,178,1,168.1255,0.04637 },
{ 1,179,1,168.5482,0.04628 },
{ 1,180,1,168.958,0.04619 },
{ 1,181,1,169.3549,0.04609 },
{ 1,182,1,169.7389,0.04599 },
{ 1,183,1,170.1099,0.04589 },
{ 1,184,1,170.468,0.04579 },
{ 1,185,1,170.8136,0.04569 },
{ 1,186,1,171.1468,0.04559 },
{ 1,187,1,171.468,0.04548 },
{ 1,188,1,171.7773,0.04538 },
{ 1,189,1,172.0748,0.04527 },
{ 1,190,1,172.3606,0.04516 },
{ 1,191,1,172.6345,0.04506 },
{ 1,192,1,172.8967,0.04495 },
{ 1,193,1,173.147,0.04484 },
{ 1,194,1,173.3856,0.04473 },
{ 1,195,1,173.6126,0.04462 },
{ 1,196,1,173.828,0.04451 },
{ 1,197,1,174.0321,0.0444 },
{ 1,198,1,174.2251,0.04429 },
{ 1,199,1,174.4071,0.04418 },
{ 1,200,1,174.5784,0.04407 },
{ 1,201,1,174.7392,0.04396 },
{ 1,202,1,174.8896,0.04385 },
{ 1,203,1,175.0301,0.04375 },
{ 1,204,1,175.1609,0.04364 },
{ 1,205,1,175.2824,0.04353 },
{ 1,206,1,175.3951,0.04343 },
{ 1,207,1,175.4995,0.04332 },
{ 1,208,1,175.5959,0.04322 },
{ 1,209,1,175.685,0.04311 },
{ 1,210,1,175.7672,0.04301 },
{ 1,211,1,175.8432,0.04291 },
{ 1,212,1,175.9133,0.04281 },
{ 1,213,1,175.9781,0.04271 },
{ 1,214,1,176.038,0.04261 },
{ 1,215,1,176.0935,0.04251 },
{ 1,216,1,176.1449,0.04241 },
{ 1,217,1,176.1925,0.04232 },
{ 1,218,1,176.2368,0.04222 },
{ 1,219,1,176.2779,0.04213 },
{ 1,220,1,176.3162,0.04204 },
{ 1,221,1,176.3518,0.04195 },
{ 1,222,1,176.3851,0.04185 },
{ 1,223,1,176.4162,0.04177 },
{ 1,224,1,176.4453,0.04168 },
{ 1,225,1,176.4724,0.04159 },
{ 1,226,1,176.4976,0.0415 },
{ 1,227,1,176.5211,0.04142 },
{ 1,228,1,176.5432,0.04134 },
{ 2,0,1,49.1477,0.0379 },
{ 2,1,1,53.6872,0.0364 },
{ 2,2,1,57.0673,0.03568 },
{ 2,3,1,59.8029,0.0352 },
{ 2,4,1,62.0899,0.03486 },
{ 2,5,1,64.0301,0.03463 },
{ 2,6,1,65.7311,0.03448 },
{ 2,7,1,67.2873,0.03441 },
{ 2,8,1,68.7498,0.0344 },
{ 2,9,1,70.1435,0.03444 },
{ 2,10,1,71.4818,0.03452 },
{ 2,11,1,72.771,0.03464 },
{ 2,12,1,74.015,0.03479 },
{ 2,13,1,75.2176,0.03496 },
{ 2,14,1,76.3817,0.03514 },
{ 2,15,1,77.5099,0.03534 },
{ 2,16,1,78.6055,0.03555 },
{ 2,17,1,79.671,0.03576 },
{ 2,18,1,80.7079,0.03598 },
{ 2,19,1,81.7182,0.0362 },
{ 2,20,1,82.7036,0.03643 },
{ 2,21,1,83.6654,0.03666 },
{ 2,22,1,84.604,0.03688 },
{ 2,23,1,85.5202,0.03711 },
{ 2,24,1,86.4153,0.03734 },
{ 2,24,1,85.7153,0.03764 },
{ 2,25,1,86.5904,0.03786 },
{ 2,26,1,87.4462,0.03808 },
{ 2,27,1,88.283,0.0383 },
{ 2,28,1,89.1004,0.03851 },
{ 2,29,1,89.8991,0.03872 },
{ 2,30,1,90.6797,0.03893 },
{ 2,31,1,91.443,0.03913 },
{ 2,32,1,92.1906,0.03933 },
{ 2,33,1,92.9239,0.03952 },
{ 2,34,1,93.6444,0.03971 },
{ 2,35,1,94.3533,0.03989 },
{ 2,36,1,95.0515,0.04006 },
{ 2,37,1,95.7399,0.04024 },
{ 2,38,1,96.4187,0.04041 },
{ 2,39,1,97.0885,0.04057 },
{ 2,40,1,97.7493,0.04073 },
{ 2,41,1,98.4015,0.04089 },
{ 2,42,1,99.0448,0.04105 },
{ 2,43,1,99.6795,0.0412 },
{ 2,44,1,100.3058,0.04135 },
{ 2,45,1,100.9238,0.0415 },
{ 2,46,1,101.5337,0.04164 },
{ 2,47,1,102.136,0.04179 },
{ 2,48,1,102.7312,0.04193 },
{ 2,49,1,103.3197,0.04206 },
{ 2,50,1,103.9021,0.0422 },
{ 2,51,1,104.4786,0.04233 },
{ 2,52,1,105.0494,0.04246 },
{ 2,53,1,105.6148,0.04259 },
{ 2,54,1,106.1748,0.04272 },
{ 2,55,1,106.7295,0.04285 },
{ 2,56,1,107.2788,0.04298 },
{ 2,57,1,107.8227,0.0431 },
{ 2,58,1,108.3613,0.04322 },
{ 2,59,1,108.8948,0.04334 },
{ 2,60,1,109.4233,0.04347 },
{ 2,61,1,109.6016,0.04355 },
{ 2,62,1,110.1258,0.04364 },
{ 2,63,1,110.6451,0.04373 },
{ 2,64,1,111.1596,0.04382 },
{ 2,65,1,111.6696,0.0439 },
{ 2,66,1,112.1753,0.04399 },
{ 2,67,1,112.6767,0.04407 },
{ 2,68,1,113.174,0.04415 },
{ 2,69,1,113.6672,0.04423 },
{ 2,70,1,114.1565,0.04431 },
{ 2,71,1,114.6421,0.04439 },
{ 2,72,1,115.1244,0.04447 },
{ 2,73,1,115.6039,0.04454 },
{ 2,74,1,116.0812,0.04461 },
{ 2,75,1,116.5568,0.04469 },
{ 2,76,1,117.0311,0.04475 },
{ 2,77,1,117.5044,0.04482 },
{ 2,78,1,117.9769,0.04489 },
{ 2,79,1,118.4489,0.04495 },
{ 2,80,1,118.9208,0.04502 },
{ 2,81,1,119.3926,0.04508 },
{ 2,82,1,119.8648,0.04514 },
{ 2,83,1,120.3374,0.0452 },
{ 2,84,1,120.8105,0.04525 },
{ 2,85,1,121.2843,0.04531 },
{ 2,86,1,121.7587,0.04536 },
{ 2,87,1,122.2338,0.04542 },
{ 2,88,1,122.7098,0.04547 },
{ 2,89,1,123.1868,0.04551 },
{ 2,90,1,123.6646,0.04556 },
{ 2,91,1,124.1435,0.04561 },
{ 2,92,1,124.6234,0.04565 },
{ 2,93,1,125.1045,0.04569 },
{ 2,94,1,125.5869,0.04573 },
{ 2,95,1,126.0706,0.04577 },
{ 2,96,1,126.5558,0.04581 },
{ 2,97,1,127.0424,0.04585 },
{ 2,98,1,127.5304,0.04588 },
{ 2,99,1,128.0199,0.04591 },
{ 2,100,1,128.5109,0.04594 },
{ 2,101,1,129.0035,0.04597 },
{ 2,102,1,129.4975,0.046 },
{ 2,103,1,129.9932,0.04602 },
{ 2,104,1,130.4904,0.04604 },
{ 2,105,1,130.9891,0.04607 },
{ 2,106,1,131.4895,0.04608 },
{ 2,107,1,131.9912,0.0461 },
{ 2,108,1,132.4944,0.04612 },
{ 2,109,1,132.9989,0.04613 },
{ 2,110,1,133.5046,0.04614 },
{ 2,111,1,134.0118,0.04615 },
{ 2,112,1,134.5202,0.04616 },
{ 2,113,1,135.0299,0.04616 },
{ 2,114,1,135.541,0.04617 },
{ 2,115,1,136.0533,0.04617 },
{ 2,116,1,136.567,0.04616 },
{ 2,117,1,137.0821,0.04616 },
{ 2,118,1,137.5987,0.04616 },
{ 2,119,1,138.1167,0.04615 },
{ 2,120,1,138.6363,0.04614 },
{ 2,121,1,139.1575,0.04612 },
{ 2,122,1,139.6803,0.04611 },
{ 2,123,1,140.2049,0.04609 },
{ 2,124,1,140.7313,0.04607 },
{ 2,125,1,141.2594,0.04605 },
{ 2,126,1,141.7892,0.04603 },
{ 2,127,1,142.3206,0.046 },
{ 2,128,1,142.8534,0.04597 },
{ 2,129,1,143.3874,0.04594 },
{ 2,130,1,143.9222,0.04591 },
{ 2,131,1,144.4575,0.04588 },
{ 2,132,1,144.9929,0.04584 },
{ 2,133,1,145.528,0.0458 },
{ 2,134,1,146.0622,0.04576 },
{ 2,135,1,146.5951,0.04571 },
{ 2,136,1,147.1262,0.04567 },
{ 2,137,1,147.6548,0.04562 },
{ 2,138,1,148.1804,0.04557 },
{ 2,139,1,148.7023,0.04552 },
{ 2,140,1,149.2197,0.04546 },
{ 2,141,1,149.7322,0.04541 },
{ 2,142,1,150.239,0.04535 },
{ 2,143,1,150.7394,0.04529 },
{ 2,144,1,151.2327,0.04523 },
{ 2,145,1,151.7182,0.04516 },
{ 2,146,1,152.1951,0.0451 },
{ 2,147,1,152.6628,0.04503 },
{ 2,148,1,153.1206,0.04497 },
{ 2,149,1,153.5678,0.0449 },
{ 2,150,1,154.0041,0.04483 },
{ 2,151,1,154.429,0.04476 },
{ 2,152,1,154.8423,0.04468 },
{ 2,153,1,155.2437,0.04461 },
{ 2,154,1,155.633,0.04454 },
{ 2,155,1,156.0101,0.04446 },
{ 2,156,1,156.3748,0.04439 },
{ 2,157,1,156.7269,0.04431 },
{ 2,158,1,157.0666,0.04423 },
{ 2,159,1,157.3936,0.04415 },
{ 2,160,1,157.7082,0.04408 },
{ 2,161,1,158.0102,0.044 },
{ 2,162,1,158.2997,0.04392 },
{ 2,163,1,158.5771,0.04384 },
{ 2,164,1,158.8425,0.04376 },
{ 2,165,1,159.0961,0.04369 },
{ 2,166,1,159.3382,0.04361 },
{ 2,167,1,159.5691,0.04353 },
{ 2,168,1,159.789,0.04345 },
{ 2,169,1,159.9983,0.04337 },
{ 2,170,1,160.1971,0.0433 },
{ 2,171,1,160.3857,0.04322 },
{ 2,172,1,160.5643,0.04314 },
{ 2,173,1,160.7332,0.04307 },
{ 2,174,1,160.8927,0.04299 },
{ 2,175,1,161.043,0.04292 },
{ 2,176,1,161.1845,0.04284 },
{ 2,177,1,161.3176,0.04277 },
{ 2,178,1,161.4425,0.0427 },
{ 2,179,1,161.5596,0.04263 },
{ 2,180,1,161.6692,0.04255 },
{ 2,181,1,161.7717,0.04248 },
{ 2,182,1,161.8673,0.04241 },
{ 2,183,1,161.9564,0.04235 },
{ 2,184,1,162.0393,0.04228 },
{ 2,185,1,162.1164,0.04221 },
{ 2,186,1,162.188,0.04214 },
{ 2,187,1,162.2542,0.04208 },
{ 2,188,1,162.3154,0.04201 },
{ 2,189,1,162.3719,0.04195 },
{ 2,190,1,162.4239,0.04189 },
{ 2,191,1,162.4717,0.04182 },
{ 2,192,1,162.5156,0.04176 },
{ 2,193,1,162.556,0.0417 },
{ 2,194,1,162.5933,0.04164 },
{ 2,195,1,162.6276,0.04158 },
{ 2,196,1,162.6594,0.04152 },
{ 2,197,1,162.689,0.04147 },
{ 2,198,1,162.7165,0.04141 },
{ 2,199,1,162.7425,0.04136 },
{ 2,200,1,162.767,0.0413 },
{ 2,201,1,162.7904,0.04125 },
{ 2,202,1,162.8126,0.04119 },
{ 2,203,1,162.834,0.04114 },
{ 2,204,1,162.8545,0.04109 },
{ 2,205,1,162.8743,0.04104 },
{ 2,206,1,162.8935,0.04099 },
{ 2,207,1,162.912,0.04094 },
{ 2,208,1,162.93,0.04089 },
{ 2,209,1,162.9476,0.04084 },
{ 2,210,1,162.9649,0.0408 },
{ 2,211,1,162.9817,0.04075 },
{ 2,212,1,162.9983,0.04071 },
{ 2,213,1,163.0144,0.04066 },
{ 2,214,1,163.03,0.04062 },
{ 2,215,1,163.0451,0.04058 },
{ 2,216,1,163.0595,0.04053 },
{ 2,217,1,163.0733,0.04049 },
{ 2,218,1,163.0862,0.04045 },
{ 2,219,1,163.0982,0.04041 },
{ 2,220,1,163.1092,0.04037 },
{ 2,221,1,163.1192,0.04034 },
{ 2,222,1,163.1279,0.0403 },
{ 2,223,1,163.1355,0.04026 },
{ 2,224,1,163.1418,0.04023 },
{ 2,225,1,163.1469,0.04019 },
{ 2,226,1,163.1508,0.04016 },
{ 2,227,1,163.1534,0.04012 },
{ 2,228,1,163.1548,0.04009 }
                                              };
        private static readonly double[,] WHO2007WtAge = {
{ 1,0,0.3487,3.3464,0.14602 },
{ 1,1,0.2297,4.4709,0.13395 },
{ 1,2,0.197,5.5675,0.12385 },
{ 1,3,0.1738,6.3762,0.11727 },
{ 1,4,0.1553,7.0023,0.11316 },
{ 1,5,0.1395,7.5105,0.1108 },
{ 1,6,0.1257,7.934,0.10958 },
{ 1,7,0.1134,8.297,0.10902 },
{ 1,8,0.1021,8.6151,0.10882 },
{ 1,9,0.0917,8.9014,0.10881 },
{ 1,10,0.082,9.1649,0.10891 },
{ 1,11,0.073,9.4122,0.10906 },
{ 1,12,0.0644,9.6479,0.10925 },
{ 1,13,0.0563,9.8749,0.10949 },
{ 1,14,0.0487,10.0953,0.10976 },
{ 1,15,0.0413,10.3108,0.11007 },
{ 1,16,0.0343,10.5228,0.11041 },
{ 1,17,0.0275,10.7319,0.11079 },
{ 1,18,0.0211,10.9385,0.11119 },
{ 1,19,0.0148,11.143,0.11164 },
{ 1,20,0.0087,11.3462,0.11211 },
{ 1,21,0.0029,11.5486,0.11261 },
{ 1,22,-0.0028,11.7504,0.11314 },
{ 1,23,-0.0083,11.9514,0.11369 },
{ 1,24,-0.0137,12.1515,0.11426 },
{ 1,25,-0.0189,12.3502,0.11485 },
{ 1,26,-0.024,12.5466,0.11544 },
{ 1,27,-0.0289,12.7401,0.11604 },
{ 1,28,-0.0337,12.9303,0.11664 },
{ 1,29,-0.0385,13.1169,0.11723 },
{ 1,30,-0.0431,13.3,0.11781 },
{ 1,31,-0.0476,13.4798,0.11839 },
{ 1,32,-0.052,13.6567,0.11896 },
{ 1,33,-0.0564,13.8309,0.11953 },
{ 1,34,-0.0606,14.0031,0.12008 },
{ 1,35,-0.0648,14.1736,0.12062 },
{ 1,36,-0.0689,14.3429,0.12116 },
{ 1,37,-0.0729,14.5113,0.12168 },
{ 1,38,-0.0769,14.6791,0.1222 },
{ 1,39,-0.0808,14.8466,0.12271 },
{ 1,40,-0.0846,15.014,0.12322 },
{ 1,41,-0.0883,15.1813,0.12373 },
{ 1,42,-0.092,15.3486,0.12425 },
{ 1,43,-0.0957,15.5158,0.12478 },
{ 1,44,-0.0993,15.6828,0.12531 },
{ 1,45,-0.1028,15.8497,0.12586 },
{ 1,46,-0.1063,16.0163,0.12643 },
{ 1,47,-0.1097,16.1827,0.127 },
{ 1,48,-0.1131,16.3489,0.12759 },
{ 1,49,-0.1165,16.515,0.12819 },
{ 1,50,-0.1198,16.6811,0.1288 },
{ 1,51,-0.123,16.8471,0.12943 },
{ 1,52,-0.1262,17.0132,0.13005 },
{ 1,53,-0.1294,17.1792,0.13069 },
{ 1,54,-0.1325,17.3452,0.13133 },
{ 1,55,-0.1356,17.5111,0.13197 },
{ 1,56,-0.1387,17.6768,0.13261 },
{ 1,57,-0.1417,17.8422,0.13325 },
{ 1,58,-0.1447,18.0073,0.13389 },
{ 1,59,-0.1477,18.1722,0.13453 },
{ 1,60,-0.1506,18.3366,0.13517 },
{ 1,61,-0.2026,18.5057,0.12988 },
{ 1,62,-0.213,18.6802,0.13028 },
{ 1,63,-0.2234,18.8563,0.13067 },
{ 1,64,-0.2338,19.034,0.13105 },
{ 1,65,-0.2443,19.2132,0.13142 },
{ 1,66,-0.2548,19.394,0.13178 },
{ 1,67,-0.2653,19.5765,0.13213 },
{ 1,68,-0.2758,19.7607,0.13246 },
{ 1,69,-0.2864,19.9468,0.13279 },
{ 1,70,-0.2969,20.1344,0.13311 },
{ 1,71,-0.3075,20.3235,0.13342 },
{ 1,72,-0.318,20.5137,0.13372 },
{ 1,73,-0.3285,20.7052,0.13402 },
{ 1,74,-0.339,20.8979,0.13432 },
{ 1,75,-0.3494,21.0918,0.13462 },
{ 1,76,-0.3598,21.287,0.13493 },
{ 1,77,-0.3701,21.4833,0.13523 },
{ 1,78,-0.3804,21.681,0.13554 },
{ 1,79,-0.3906,21.8799,0.13586 },
{ 1,80,-0.4007,22.08,0.13618 },
{ 1,81,-0.4107,22.2813,0.13652 },
{ 1,82,-0.4207,22.4837,0.13686 },
{ 1,83,-0.4305,22.6872,0.13722 },
{ 1,84,-0.4402,22.8915,0.13759 },
{ 1,85,-0.4499,23.0968,0.13797 },
{ 1,86,-0.4594,23.3029,0.13838 },
{ 1,87,-0.4688,23.5101,0.1388 },
{ 1,88,-0.4781,23.7182,0.13923 },
{ 1,89,-0.4873,23.9272,0.13969 },
{ 1,90,-0.4964,24.1371,0.14016 },
{ 1,91,-0.5053,24.3479,0.14065 },
{ 1,92,-0.5142,24.5595,0.14117 },
{ 1,93,-0.5229,24.7722,0.1417 },
{ 1,94,-0.5315,24.9858,0.14226 },
{ 1,95,-0.5399,25.2005,0.14284 },
{ 1,96,-0.5482,25.4163,0.14344 },
{ 1,97,-0.5564,25.6332,0.14407 },
{ 1,98,-0.5644,25.8513,0.14472 },
{ 1,99,-0.5722,26.0706,0.14539 },
{ 1,100,-0.5799,26.2911,0.14608 },
{ 1,101,-0.5873,26.5128,0.14679 },
{ 1,102,-0.5946,26.7358,0.14752 },
{ 1,103,-0.6017,26.9602,0.14828 },
{ 1,104,-0.6085,27.1861,0.14905 },
{ 1,105,-0.6152,27.4137,0.14984 },
{ 1,106,-0.6216,27.6432,0.15066 },
{ 1,107,-0.6278,27.875,0.15149 },
{ 1,108,-0.6337,28.1092,0.15233 },
{ 1,109,-0.6393,28.3459,0.15319 },
{ 1,110,-0.6446,28.5854,0.15406 },
{ 1,111,-0.6496,28.8277,0.15493 },
{ 1,112,-0.6543,29.0731,0.15581 },
{ 1,113,-0.6585,29.3217,0.1567 },
{ 1,114,-0.6624,29.5736,0.1576 },
{ 1,115,-0.6659,29.8289,0.1585 },
{ 1,116,-0.6689,30.0877,0.1594 },
{ 1,117,-0.6714,30.3501,0.16031 },
{ 1,118,-0.6735,30.616,0.16122 },
{ 1,119,-0.6752,30.8854,0.16213 },
{ 1,120,-0.6764,31.1586,0.16305 },
{ 2,0,0.3809,3.2322,0.14171 },
{ 2,1,0.1714,4.1873,0.13724 },
{ 2,2,0.0962,5.1282,0.13 },
{ 2,3,0.0402,5.8458,0.12619 },
{ 2,4,-0.005,6.4237,0.12402 },
{ 2,5,-0.043,6.8985,0.12274 },
{ 2,6,-0.0756,7.297,0.12204 },
{ 2,7,-0.1039,7.6422,0.12178 },
{ 2,8,-0.1288,7.9487,0.12181 },
{ 2,9,-0.1507,8.2254,0.12199 },
{ 2,10,-0.17,8.48,0.12223 },
{ 2,11,-0.1872,8.7192,0.12247 },
{ 2,12,-0.2024,8.9481,0.12268 },
{ 2,13,-0.2158,9.1699,0.12283 },
{ 2,14,-0.2278,9.387,0.12294 },
{ 2,15,-0.2384,9.6008,0.12299 },
{ 2,16,-0.2478,9.8124,0.12303 },
{ 2,17,-0.2562,10.0226,0.12306 },
{ 2,18,-0.2637,10.2315,0.12309 },
{ 2,19,-0.2703,10.4393,0.12315 },
{ 2,20,-0.2762,10.6464,0.12323 },
{ 2,21,-0.2815,10.8534,0.12335 },
{ 2,22,-0.2862,11.0608,0.1235 },
{ 2,23,-0.2903,11.2688,0.12369 },
{ 2,24,-0.2941,11.4775,0.1239 },
{ 2,25,-0.2975,11.6864,0.12414 },
{ 2,26,-0.3005,11.8947,0.12441 },
{ 2,27,-0.3032,12.1015,0.12472 },
{ 2,28,-0.3057,12.3059,0.12506 },
{ 2,29,-0.308,12.5073,0.12545 },
{ 2,30,-0.3101,12.7055,0.12587 },
{ 2,31,-0.312,12.9006,0.12633 },
{ 2,32,-0.3138,13.093,0.12683 },
{ 2,33,-0.3155,13.2837,0.12737 },
{ 2,34,-0.3171,13.4731,0.12794 },
{ 2,35,-0.3186,13.6618,0.12855 },
{ 2,36,-0.3201,13.8503,0.12919 },
{ 2,37,-0.3216,14.0385,0.12988 },
{ 2,38,-0.323,14.2265,0.13059 },
{ 2,39,-0.3243,14.414,0.13135 },
{ 2,40,-0.3257,14.601,0.13213 },
{ 2,41,-0.327,14.7873,0.13293 },
{ 2,42,-0.3283,14.9727,0.13376 },
{ 2,43,-0.3296,15.1573,0.1346 },
{ 2,44,-0.3309,15.341,0.13545 },
{ 2,45,-0.3322,15.524,0.1363 },
{ 2,46,-0.3335,15.7064,0.13716 },
{ 2,47,-0.3348,15.8882,0.138 },
{ 2,48,-0.3361,16.0697,0.13884 },
{ 2,49,-0.3374,16.2511,0.13968 },
{ 2,50,-0.3387,16.4322,0.14051 },
{ 2,51,-0.34,16.6133,0.14132 },
{ 2,52,-0.3414,16.7942,0.14213 },
{ 2,53,-0.3427,16.9748,0.14293 },
{ 2,54,-0.344,17.1551,0.14371 },
{ 2,55,-0.3453,17.3347,0.14448 },
{ 2,56,-0.3466,17.5136,0.14525 },
{ 2,57,-0.3479,17.6916,0.146 },
{ 2,58,-0.3492,17.8686,0.14675 },
{ 2,59,-0.3505,18.0445,0.14748 },
{ 2,60,-0.3518,18.2193,0.14821 },
{ 2,61,-0.4681,18.2579,0.14295 },
{ 2,62,-0.4711,18.4329,0.1435 },
{ 2,63,-0.4742,18.6073,0.14404 },
{ 2,64,-0.4773,18.7811,0.14459 },
{ 2,65,-0.4803,18.9545,0.14514 },
{ 2,66,-0.4834,19.1276,0.14569 },
{ 2,67,-0.4864,19.3004,0.14624 },
{ 2,68,-0.4894,19.473,0.14679 },
{ 2,69,-0.4924,19.6455,0.14735 },
{ 2,70,-0.4954,19.818,0.1479 },
{ 2,71,-0.4984,19.9908,0.14845 },
{ 2,72,-0.5013,20.1639,0.149 },
{ 2,73,-0.5043,20.3377,0.14955 },
{ 2,74,-0.5072,20.5124,0.1501 },
{ 2,75,-0.51,20.6885,0.15065 },
{ 2,76,-0.5129,20.8661,0.1512 },
{ 2,77,-0.5157,21.0457,0.15175 },
{ 2,78,-0.5185,21.2274,0.1523 },
{ 2,79,-0.5213,21.4113,0.15284 },
{ 2,80,-0.524,21.5979,0.15339 },
{ 2,81,-0.5268,21.7872,0.15393 },
{ 2,82,-0.5294,21.9795,0.15448 },
{ 2,83,-0.5321,22.1751,0.15502 },
{ 2,84,-0.5347,22.374,0.15556 },
{ 2,85,-0.5372,22.5762,0.1561 },
{ 2,86,-0.5398,22.7816,0.15663 },
{ 2,87,-0.5423,22.9904,0.15717 },
{ 2,88,-0.5447,23.2025,0.1577 },
{ 2,89,-0.5471,23.418,0.15823 },
{ 2,90,-0.5495,23.6369,0.15876 },
{ 2,91,-0.5518,23.8593,0.15928 },
{ 2,92,-0.5541,24.0853,0.1598 },
{ 2,93,-0.5563,24.3149,0.16032 },
{ 2,94,-0.5585,24.5482,0.16084 },
{ 2,95,-0.5606,24.7853,0.16135 },
{ 2,96,-0.5627,25.0262,0.16186 },
{ 2,97,-0.5647,25.271,0.16237 },
{ 2,98,-0.5667,25.5197,0.16287 },
{ 2,99,-0.5686,25.7721,0.16337 },
{ 2,100,-0.5704,26.0284,0.16386 },
{ 2,101,-0.5722,26.2883,0.16435 },
{ 2,102,-0.574,26.5519,0.16483 },
{ 2,103,-0.5757,26.819,0.16532 },
{ 2,104,-0.5773,27.0896,0.16579 },
{ 2,105,-0.5789,27.3635,0.16626 },
{ 2,106,-0.5804,27.6406,0.16673 },
{ 2,107,-0.5819,27.9208,0.16719 },
{ 2,108,-0.5833,28.204,0.16764 },
{ 2,109,-0.5847,28.4901,0.16809 },
{ 2,110,-0.5859,28.7791,0.16854 },
{ 2,111,-0.5872,29.0711,0.16897 },
{ 2,112,-0.5883,29.3663,0.16941 },
{ 2,113,-0.5895,29.6646,0.16983 },
{ 2,114,-0.5905,29.9663,0.17025 },
{ 2,115,-0.5915,30.2715,0.17066 },
{ 2,116,-0.5925,30.5805,0.17107 },
{ 2,117,-0.5934,30.8934,0.17146 },
{ 2,118,-0.5942,31.2105,0.17186 },
{ 2,119,-0.595,31.5319,0.17224 },
{ 2,120,-0.5958,31.8578,0.17262 }
                                              };
        #endregion WHO 2007 Reference Data

        public static double GetZScore(Indicator indicator, double firstMeasurement, double secondMeasurement, int gender, ref double flag)
        {
            // pre
            Contract.Requires(indicator == Indicator.BMIForAge || indicator == Indicator.HeightForAge || indicator == Indicator.WeightForAge);
            Contract.Requires(firstMeasurement >= 0);
            Contract.Requires(secondMeasurement >= 0);
            Contract.Requires(gender >= 1);
            Contract.Requires(gender <= 2);

            switch (indicator)
            {
                case Indicator.BMIForAge:
                    return GetZ_WHO2007(firstMeasurement, secondMeasurement, gender, 0, ref flag);
                case Indicator.HeightForAge:
                    return GetZ_WHO2007(firstMeasurement, secondMeasurement, gender, 2, ref flag);
                case Indicator.WeightForAge:
                    return GetZ_WHO2007(firstMeasurement, secondMeasurement, gender, 4, ref flag);
                default:
                    throw new InvalidOperationException("Unsupported indicator type for WHO 2007 reference.");
            }
        }

        private static double GetZ_WHO2007(double rawScore, double ageMonths, int gender, int measurementType, ref double flag)
        {
            double L = -1;
            double M = -1;
            double S = -1;
            double Z = 1;
            double[] ZLMS = new double[4];
            bool foundValue = false;

            double[,] reference;

            switch (measurementType)
            {
                case 0:
                    reference = WHO2007BMI;
                    break;
                case 2:
                    reference = WHO2007HtAge;
                    break;
                case 4:
                    reference = WHO2007WtAge;
                    break;
                default:
                    reference = WHO2007HtAge;
                    break;
            }

            for (int i = 0; i < reference.Length / 5; i++)
            {
                if (reference[i, 0] == gender && reference[i, 1] == ageMonths)
                {
                    L = reference[i, 2];
                    M = reference[i, 3];
                    S = reference[i, 4];
                    foundValue = true;
                    break;
                }
            }

            Z = (rawScore / M);
            Z = Math.Pow(Z, L);
            Z = Z - 1;
            Z = Z / (L * S);

            if (!foundValue)
            {
                ZLMS = InterpZWHO2007(rawScore, ageMonths, gender, measurementType, ref flag);
                Z = ZLMS[0];
                L = ZLMS[1];
                M = ZLMS[2];
                S = ZLMS[3];
            }
            else
            {
                double stddev = 0;
                if (rawScore < M)
                    stddev = M * (1 - Math.Pow((1 - 2 * L * S), (1 / L))) / 2;
                else
                    stddev = M * (Math.Pow((1 + (2 * L * S)), (1 / L)) - 1) / 2;

                flag = (rawScore - M) / stddev;
            }

            // Re-calculates Z if the LMS z-score is above 3 or below -3, as per http://www.who.int/childgrowth/standards/Chap_7.pdf
            if (Z > 3)
            {
                double SD3pos = 0;
                double SD2pos = 0;
                double SD23pos = 0;

                SD3pos = M * Math.Pow((1 + L * S * 3), 1 / L);
                SD2pos = M * Math.Pow((1 + L * S * 2), 1 / L);
                SD23pos = SD3pos - SD2pos;

                Z = 3 + ((rawScore - SD3pos) / SD23pos);
            }
            else if (Z < -3)
            {
                double SD3neg = 0;
                double SD2neg = 0;
                double SD23neg = 0;

                SD3neg = M * Math.Pow((1 + L * S * (-3)), 1 / L);
                SD2neg = M * Math.Pow((1 + L * S * (-2)), 1 / L);
                SD23neg = SD2neg - SD3neg;

                Z = (-3) + ((rawScore - SD3neg) / SD23neg);
            }

            return Z;
        } // end GetZ_WHO2007()

        private static double[] InterpZWHO2007(double rawScore, double ageMonths, int gender, int measurementType, ref double flag)
        {
            double L = -1;
            double M = -1;
            double S = -1;
            double L_u = -1;
            double M_u = -1;
            double S_u = -1;
            double L_l = -1;
            double M_l = -1;
            double S_l = -1;
            double Z = 1;
            double upper = 0;
            double lower = 0;
            double difference = 0;
            double wholeMonths = Math.Truncate(ageMonths);
            difference = ageMonths - wholeMonths;

            double[,] reference;

            switch (measurementType)
            {
                case 0:
                    reference = WHO2007BMI;
                    break;
                case 2:
                    reference = WHO2007HtAge;
                    break;
                case 4:
                    reference = WHO2007WtAge;
                    break;
                default:
                    reference = WHO2007HtAge;
                    break;
            }

            if (measurementType != 5 && measurementType != 6)
            {
                lower = wholeMonths;
                upper = lower + 1.0;
            }
            else
            {
                if (difference > 0.5)
                {
                    lower = wholeMonths + 0.5;
                }
                else
                {
                    lower = wholeMonths;
                }
                upper = lower + 0.5;
            }

            for (int i = 0; i < reference.Length / 5; i++)
            {
                if (reference[i, 0] == gender && reference[i, 1] == lower)
                {
                    L_l = reference[i, 2];
                    M_l = reference[i, 3];
                    S_l = reference[i, 4];
                    break;
                }
            }

            for (int i = 0; i < reference.Length / 5; i++)
            {
                if (reference[i, 0] == gender && reference[i, 1] == upper)
                {
                    L_u = reference[i, 2];
                    M_u = reference[i, 3];
                    S_u = reference[i, 4];
                    break;
                }
            }
            double percent = (ageMonths - lower);
            double percent_u = (upper - ageMonths);

            if (measurementType == 5 || measurementType == 6)
            {
                percent = percent * 2;
                percent_u = percent_u * 2;
            }

            L = (L_u * percent) + (L_l * percent_u);
            M = (M_u * percent) + (M_l * percent_u);
            S = (S_u * percent) + (S_l * percent_u);

            // Now that we're done, we simply do the stat math to get the Z-score just like we did in GetZScore().
            Z = (rawScore / M);
            Z = Math.Pow(Z, L);
            Z = Z - 1;
            Z = Z / (L * S);

            // Do the flag calculations            
            double stddev = 0;
            if (rawScore < M)
            {
                stddev = M * (1 - Math.Pow((1 - 2 * L * S), (1 / L))) / 2;
            }
            else
            {
                stddev = M * (Math.Pow((1 + (2 * L * S)), (1 / L)) - 1) / 2;
            }

            flag = (rawScore - M) / stddev;

            // Return the z-score to the calling function.
            double[] ZLMS = { Z, L, M, S };
            return ZLMS;

        } // end InterpZWHO2007
    }
}
